﻿using System;
using System.Security.Cryptography;
using System.Web.Security;

namespace Complaint.Common
{
    /// <summary>
    /// Cryptography utility class
    /// </summary>
    public static class CryptoUtility
    {
        public static string CreateSalt(int size)
        {
            // Generate a cryptographic random number using the cryptographic
            // service provider
            var rng = new RNGCryptoServiceProvider();
            var buff = new byte[size];
            rng.GetBytes(buff);
            // Return a Base64 string representation of the random number
            return Convert.ToBase64String(buff);
        }

        /// <summary>
        /// Returns a hashed password with a specified salt value.
        /// </summary>
        /// <param name="pwd">Original password to be hased.</param>
        /// <param name="salt">Salt value.</param>
        /// <returns>Hashed password.</returns>
        public static string CreatePasswordHash(string pwd, string salt)
        {
            var saltAndPwd = String.Concat(pwd, salt);
            var hashedPwd =
             FormsAuthentication.HashPasswordForStoringInConfigFile(
             saltAndPwd, "sha1");

            return hashedPwd;
        }

    }
}
